package whf.framework.meta.util;

import java.beans.PropertyDescriptor;
import java.util.Set;

import whf.framework.entity.AbstractEntity;
import whf.framework.exception.DAOException;
import whf.framework.exception.ServiceNotFoundException;
import whf.framework.meta.entity.ObjectProperty;
import whf.framework.meta.service.ObjectService;
import whf.framework.meta.service.ObjectServiceImp;
import whf.framework.security.entity.Permission;
import whf.framework.util.BeanUtils;
import whf.framework.util.StringUtils;
import whf.framework.util.TypeUtils;
import whf.framework.web.struts.AbstractStrutsForm;

/**
 * @author wanghaifeng
 *
 */
public final class MetaUtils {
	private MetaUtils(){
	}
	
	private static String getRootPackage(Class cls){
		String p = cls.getPackage().getName();
		int pi = p.lastIndexOf(".");
		p = p.substring(0, pi);
		if(!p.endsWith(".")){
			p = p+".";
		}
		return p;
	}
	
	/**
	 * 使用类初始化元数据
	 * @modify wanghaifeng Oct 29, 2006 8:39:59 AM
	 * @param cls
	 * @throws MetaInitException
	 */
	public static void initMetaFromClass(Class cls) throws MetaInitException{
		if(cls == null || !TypeUtils.isExtendsFrom(cls, AbstractEntity.class)){
			return;
		}
		PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(cls);
		try{
			ObjectService service = ObjectServiceImp.getObjectService();
			whf.framework.meta.entity.Object obj = service.findByEntityClass(cls.getName());
			boolean newObj = false;
			if(obj == null){
				String rp = getRootPackage(cls);
				obj = new whf.framework.meta.entity.Object();
				obj.setActionClassName(rp+"web."+cls.getSimpleName()+"Action");
				obj.setBoClassName(cls.getName());
				obj.setDaoClassName(rp+"dao."+cls.getSimpleName()+"DAOImp");
				obj.setDaoName(cls.getSimpleName()+"DAO");
				obj.setServiceClassName(rp+"service."+cls.getSimpleName()+"Service");
				obj.setServiceName(cls.getSimpleName()+"Service");
				newObj = true;
			}
			Set<ObjectProperty> properties = obj.getProperties();
			for(PropertyDescriptor descriptor: descriptors){
				if(BeanUtils.hasProperty(AbstractStrutsForm.class, descriptor.getName())){
					continue;
				}
				ObjectProperty prop = null;
				for(ObjectProperty p : properties){
					if(StringUtils.equals(p.getName(), descriptor.getName())){
						prop = p;
						break;
					}
				}
				if(prop == null){
					prop = new ObjectProperty();
					((ObjectProperty)prop).setBoObject(obj);
					prop.setName(descriptor.getName());
					prop.setDataType(descriptor.getPropertyType().getName());
					prop.setDisplayType("text");
					obj.addProperty(prop);
				}
			}
			if(newObj){
				service.create(obj);
			} else {
				service.update(obj);
			}
		}catch(ServiceNotFoundException e){
			throw new MetaInitException(e);
		}catch(DAOException e){
			throw new MetaInitException(e);
		}
	}
	
	public static void main(String[] args) throws Exception{
		initMetaFromClass(Permission.class);
	}
}
